import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { UserService } from 'src/app/service/user.service';
import { YearawardService } from '../../../yearaward.service';
import { BaseService } from 'src/app/service/base.service';
import { UtilsService } from 'src/app/service/utils.service';

@Component({
  selector: 'app-fight',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss', '../../../commonList.scss'],
})
export class FightComponent implements OnInit, OnDestroy {
  @Input() timeConfig: any[] = [];
  /* ___ _ ___ */
  public deployUrl: string; /* 资源地址前缀 */
  /* ___ _ ___ */
  public flag = false;
  public reshing = null;
  public isPC = false; /* 是否pc */
  public isAndroid = false; /* 是否安卓 */
  public onRefrash: any; /* 刷新中 */
  public isRuleClose = true; /* 规则是否展开 */
  public pageSize = 30; /* 每页条数 */
  public stageId: number; //请求数据ID
  public haveOver: boolean = false; //是否已经结榜
  public nowDate: any;
  /* ___ _ ___ */
  public othersList: EndYear.IAnchor[] = []; /* 危险区 */
  public curProgress = 1; /* 第*轮 */
  public championPool: number = 0;
  public othersIndex = 0;
  public dangerIndex = 0;
  public site = 0;
  public noParty: boolean = false;
  public top = 0; // window.scrollY，用来判断弹出窗口后页面不可滚动
  public isDialog = false;
  public currentAnchorInfo: any = {
    nickname: '',
    avatarUrl: '',
    score: 0,
    rankDesc: '-',
    rank: 0,
    isMysteryMan: false,
  }; /* 当前主播信息 */
  public timeArr: any = [];
  constructor(
    private userService: UserService,
    private yearawardService: YearawardService,
    private baseService: BaseService,
    private utilsservice: UtilsService
  ) {}

  ngOnInit(): void {
    // 获取
    this.deployUrl = this.userService.getDeployUrl();
    this.isPC = this.userService.getIsPc();
    this.isAndroid = this.utilsservice.getBrowserVersion();
    this.initPage();
    // 组件刷新通信
    this.reshing = this.utilsservice.MsgSub.subscribe((res: any) => {
      this.initPage();
    });
  }
  ngOnDestroy() {
    if (this.reshing) {
      this.reshing.unsubscribe();
    }
  }

  public initPage() {
    this.getChampionPool();
  }
  // 获取冠军奖池
  public getChampionPool() {
    this.yearawardService.championPool(
      (res: any) => {
        if (res.code === 200) {
          this.championPool = res.data.championPool;
          this.site = res.data.site;
          if (this.site > 10) {
            this.overtimeRank();
          }
        }
      },
      (err: any) => {}
    );
  }
  // 获取榜单
  public overtimeRank() {
    this.yearawardService.overtimeRank(
      (res: any) => {
        if (res.code === 200) {
          this.curProgress = res.data.round;
          this.othersList = res.data.list; /* TOP3 */
          this.currentAnchorInfo = res.data.current || {};
        }
      },
      this.baseService.getCurrentId(),
      0,
      (err: any) => {}
    );
  }
  // 跳转直播间
  public gotoRoom(item: any, type: any) {
    if (
      (type === 'anchor' && !item.uid) ||
      (type === 'user' && !item.userUid)
    ) {
      return;
    }
    const obj = { ...item };
    if (type === 'anchor') {
      obj.isMysteryMan = false;
    }
    obj.livImage = item.avatarUrl;
    this.utilsservice.gotoRoom(obj, type);
  }
  // 点击弹窗
  public closeShow(val) {
    const bodyEl = document.body;
    if (val) {
      this.top = window.scrollY;
      bodyEl.style.position = 'fixed';
      bodyEl.style.top = -this.top + 'px';
    } else {
      bodyEl.style.position = '';
      bodyEl.style.top = '';
      window.scrollTo(0, this.top); // 回到原先的top
    }
    this.isDialog = val;
  }
}
